package com.ymttest.database.mongo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ymt.core.tool.Logger;
import com.ymt.database.MongoDBDriver;
import com.ymttest.utils.EnvSetup;

/************************************************************************************
 * @File name :YMatouSellerOrderAddInfoWapper.java
 * @Author : syf
 * @Date : 2016年7月28日
 * @Copyright : 洋码头
 ************************************************************************************/
public class YMatouSellerOrderAddInfoWapper {
	MongoDBDriver mdd;

	public YMatouSellerOrderAddInfoWapper() throws Exception {
		String[] addresses = EnvSetup.getSystemUrl("set5_mongoip").split(";");
		int port = Integer.valueOf(EnvSetup.getSystemUrl("set5_mongoport"));
		List<Map<String, String>> addresslist = new ArrayList<Map<String, String>>();
		for (String address : addresses) {
			HashMap m = new HashMap();
			m.put("address", address);
			m.put("port", port);
			addresslist.add(m);
		}
		mdd = new MongoDBDriver(addresslist, "SellerOrderAddInfo");
		mdd.auth("ymtsellerorderuser", "123456");

	}

	/**
	 * 选择表
	 * 
	 * @param collection
	 */
	public void selectCollection(String collection) {
		mdd.selectCollection(collection);
	}

	/**
	 * 用uid查询AccessToken
	 * 
	 * @param orderid
	 * @param sellerid
	 * @return {_id=104990299, Buyer=2956536, Seller=167, AddT=Thu May 28
	 *         21:52:01 CST 2015, PaidT=Thu May 28 22:06:03 CST 2015,
	 *         AcceptT=Tue Jun 02 09:24:29 CST 2015, DeliverT=Tue Jun 02
	 *         09:32:29 CST 2015, Type=0, TradingStatus=4, FullPaid=1,
	 *         Products=[ { "Logistics" : 1 , "PrdId" :
	 *         "1affd117-6ccf-493e-b3b4-c2fb82a7d456"}], Income=0.0, Status=400}
	 * @throws Exception
	 */
	public Map selectOrderCountBySeller(int sellerId) throws Exception {
		// /sleep一下保证mongo已经写入
		Thread.sleep(500);
		Map re = new HashMap();
		selectCollection("SellerOrdersCount");
		ArrayList<Map> list = mdd.selectPartLimitToMap("{'_id':" + sellerId
				+ "}", 1);
		try {
			if (list.size() > 0) {
				re = list.get(0);
				return re;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String args[]) {
		try {
			YMatouSellerOrderAddInfoWapper ut = new YMatouSellerOrderAddInfoWapper();
			System.out.println(ut.selectOrderCountBySeller(3383));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected void close() throws Throwable {
		mdd.close();
	}
}
